/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.jsf.components;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Fernando
 */
public class NavButtonComponent extends PravBaseComponent{

    public static final String COMPONENT_FAMILY = "PRAV_NAVBUTTON";
    public static final String COMPONENT_TYPE = "PRAV_NAVBUTTON";
    
    public NavButtonComponent(){
        
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Map<String, Object> params = context.getViewRoot().getAttributes();
        boolean found = false;
        String href = "";
        //look for a param that matches the id of THIS navButton
        for (String key : params.keySet()){
//            System.out.println("key: " + key);
            if (key.equals(this.getId())){
                found = true;
                href = (String)params.get(key);
//                System.out.println(key + ": " + href);
            }
        }

        if (found)
            params.put("preferedHref", href);
        else {
            hideInnerComponents();
            return;
        }

		ResponseWriter writer =
			context.getResponseWriter();
        writer.flush();
	}

//    private String

    @Override
    public void encodeEnd(FacesContext context)
            throws IOException {
        context.getViewRoot().getAttributes().remove("preferedHref");
        ResponseWriter writer = context.getResponseWriter();
        writer.flush();
//        return;
    }

    private void setDisplayPlataform(String target){
        String[] targets = target.split(" ");
        super.setDisplayMobile(false);
        super.setDisplayTv(false);
        super.setDisplayWeb(false);
        for (String s : targets){
            if (s.equalsIgnoreCase("mobiel"))
                super.setDisplayMobile(true);
            else if (s.equalsIgnoreCase("tvd"))
                super.setDisplayTv(true);
            else if (s.equalsIgnoreCase("web"))
                super.setDisplayWeb(true);
        }
    }

    @Override
    public void decode(FacesContext context) {
    }

    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    // </editor-fold>

}
